<?php

namespace App\Models;


use Doctrine\DBAL\Schema\Table;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Sport extends Model
{

    protected $table = 'sports_test';
    public $timestamps = false;

    //得到详细体侧
    static function getDetailSport($id)
    {
        $data = self::where('user_id', $id)
            ->orderBy('created_at', 'asc')
            ->get()
            ->toArray();
        /*三种雷达,饼图,折线图
            $res1 = [];
            $leiDa = [];
            $bin =  [];
            $zhe =  [];
          foreach ($res as $k=>$v){
            $leiDa[$k]['name'] = date('m/d',$v['created_at']);
            $leiDa[$k]['value'][] = $v['legs'];
            $leiDa[$k]['value'][] = (int)$v['upper_limb'];
            $leiDa[$k]['value'][] = (int)$v['move_flex'];
            $leiDa[$k]['value'][] = (int)$v['bounce_sen'];
            $leiDa[$k]['value'][] = (int)$v['flex'];
            $leiDa[$k]['value'][] = (int)$v['oxygen'];
            $leiDa[$k]['value'][] = (int)$v['spend'];
            $leiDa[$k]['value'][] = (int)$v['BMI'];
            $bin[$k][]['value'] = (int)$v['super'];
            $bin[$k][]['value']  = (int)$v['good'];
            $bin[$k][]['value']  = (int)$v['pass'];
            $bin[$k][]['value']  = (int)$v['poor'];


              //再来一次循环
              $num = count($res);
              for ($i=0;$i<$num;$i++){
                  $zhe['data'][$k][$i] = (int)$res[$i]['height'];
                  $zhe['data'][$k+1][$i] = (int)$res[$i]['weight'];
                  $zhe['data'][$k+2][$i] = (int)$res[$i]['BMI'];
                  $zhe['data'][$k+3][$i] = (int)$res[$i]['arm_spread'];
                  $zhe['data'][$k+4][$i] = (int)$res[$i]['finger_span'];
                  $zhe['data'][$k+5][$i] = (int)$res[$i]['sprint'];
                  $zhe['data'][$k+6][$i] = (int)$res[$i]['back_forth'];
                  $zhe['data'][$k+7][$i] = (int)$res[$i]['sitting_position'];
                  $zhe['data'][$k+8][$i] = (int)$res[$i]['jump'];
                  $zhe['data'][$k+9][$i] = (int)$res[$i]['sensitive'];
                  $zhe['data'][$k+10][$i] = (int)$res[$i]['toss_ball'];
                  $zhe['data'][$k+11][$i] = (int)$res[$i]['vertical_jump'];
              }
              for ($i=0;$i<$num-1;$i++){
                  unset($zhe['data'][$i]);
              }
              $zhe['time'][] = date('m/d',$v['created_at']);

        }
        //饼图
          $res1['leiDa'] =$leiDa;
          $res1['bin'] =$bin;
          $res1['zhe'] = $zhe['data'];
  //        $res1['zhe'] = count($zhe['data']);/
        return $res1 = $res1 ?? '';
      }

      static function switchSport($id,$status){
        $res = User::where('id',$id)->update(['sport_status'=>$status]);
        return $res = true ?? false;
      }*/
        return [
            'leida' => self::leida($data),
            'bin' => self::bin($data),
            'zhexian' => self::zhexian($data)
        ];

    }

// 雷达数据
    static function leida($data)
    {
        $param = ['legs', 'upper_limb', 'move_flex', 'bounce_sen', 'flex', 'oxygen', 'spend', 'BMI'];
        $res = [];
        foreach ($data as $k => $v) {
            $res[$k]['name'] = date('m/d', $v['created_at']);
            foreach ($param as $p) {
                $res[$k]['value'][] = (float)$v[$p];
            }
        }
        return $res;
    }

// 饼图数据
    static function bin($data)
    {
        $param = ['super', 'good', 'pass', 'poor'];
        $left=['a','b','c','d'];
        $key = [0, count($data) - 1];
        $res = [];
        $num = 0;
        foreach ($data as $k => $v) {
            if (in_array($k, $key)) {
                foreach ($param as $o=>$p) {
                    $res[$num][] = [ 'name' => $left[$o] .'.'. (string)$v[$p].'%', 'value' => (float)$v[$p]];
                }
                $num++;
            }
        }
        return $res;
    }

// 折线图数据
    static function zhexian($data)
    {
        $param = ['height', 'weight', 'BMI', 'arm_spread', 'finger_span', 'sprint', 'back_forth', 'sitting_position', 'jump', 'sensitive', 'toss_ball', 'vertical_jump'];
        $res = [];
        foreach ($data as $v) {
            $res['time'][] = date('m/d', $v['created_at']);
            foreach ($param as $k => $p) {
                $res['data'][$k][] = (float)$v[$p];
            }
        }
        return $res;
    }


}

